/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.forumdata;

import domain.forumdata.tag.Tag;
import domain.settings.Settings;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 * Represents a post.
 */
public class Post implements Serializable
{
    private Category cat;
    private String topicTitle;
    private String topicSubtitle;
    private Tag tag;
    private String post;
    private ArrayList<String> topicURLResults;
    private String date;

    /**
     * Default constructor.
     * @param cat The cateogry.
     * @param topicTitle The topic title.
     * @param topicSubtitle The topic subtitle or small description.
     * @param tag The tag.
     * @param post The message.
     */
    public Post(Category cat, String topicTitle, String topicSubtitle, Tag tag, String post)
    {
        this.cat = cat;
        this.topicTitle = topicTitle;
        this.topicSubtitle = topicSubtitle;
        this.tag = tag;
        this.post = post;
        this.topicURLResults = new ArrayList<String>();
        this.date = new SimpleDateFormat(Settings.DATE_FORMAT).format(Calendar.getInstance().getTime());
    }

    /**
     * Updates the date of the post to the current date.
     */
    public void updatePost()
    {
        this.date = new SimpleDateFormat(Settings.DATE_FORMAT).format(Calendar.getInstance().getTime());
    }

    /**
     * @return The category.
     */
    public Category getCat()
    {
        return cat;
    }

    /**
     * @return The message.
     */
    public String getPost()
    {
        return post;
    }

    /**
     * @return The tag.
     */
    public Tag getTag()
    {
        return tag;
    }

    /**
     * @return The topic subtitle or small description.
     */
    public String getTopicSubtitle()
    {
        return topicSubtitle;
    }

    /**
     * @return the topic title.
     */
    public String getTopicTitle()
    {
        return topicTitle;
    }

    /**
     * @return A list containing the result URLs.
     */
    public ArrayList<String> getTopicURLResults()
    {
        return topicURLResults;
    }

    /**
     * @return The date.
     */
    public String getDate()
    {
        return date;
    }

    /**
     * @return The display title of this post. Format: topicTitle + " (" + date + ")"
     */
    public String getDisplayTitle()
    {
        return this.topicTitle + " (" + this.date + ")";
    }

    /**
     * Searches if a string occurs in this post object.
     * @param text The text that needs to occur.
     * @return True if found, false otherwise.
     */
    public boolean containsText(String text)
    {
        boolean value = false;

        if(this.cat.getName().toLowerCase().contains(text))
            value = true;

        if(this.date.toLowerCase().contains(text))
            value = true;

        if(this.post.toLowerCase().contains(text))
            value = true;

        if(this.tag.getTagName().toLowerCase().contains(text))
            value = true;

        for(String s : this.tag.getSynonyms())
            if(s.toLowerCase().contains(text))
                value = true;

        if(this.topicSubtitle.toLowerCase().contains(text))
            value = true;

        if(this.topicTitle.toLowerCase().contains(text))
            value = true;

        if(this.topicURLResults.contains(text))
            value = true;

        return value;
    }
}